package com.mobilepay.moneypig.model.activity.mine;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mobilepay.moneypig.R;
import com.mobilepay.moneypig.base.BaseActivity;
import com.mobilepay.moneypig.model.fragment.IncomeListFragment;
import com.mobilepay.moneypig.model.fragment.PayListFragment;
import com.mobilepay.moneypig.utils.CommonUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xlm on 2017/7/24 0024
 */

public class IncomeListActivity extends BaseActivity {
    private static final String TAG = "IncomeListActivity";
    private RelativeLayout mRelativeListBack;
    private LinearLayout mTopLine;
    private TextView mTextShouru;
    private TextView mTextZhichu;
    private ViewPager mViewPager;

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.activity_income_list);
    }

    @Override
    protected void initViews() {
        mRelativeListBack = (RelativeLayout) findViewById(R.id.income_list_back);
        mTextShouru = (TextView) findViewById(R.id.income_shouru);
        mTextZhichu = (TextView) findViewById(R.id.income_zhichu);
        mViewPager = (ViewPager) findViewById(R.id.income_viewpager);
        mTopLine = (LinearLayout) findViewById(R.id.topLine);
    }

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {
        initList();
        setmViewPagerAdapter();
    }

    private List<Fragment> list;

    private void initList() {
        list = new ArrayList<>();
        if (fragmentShouru == null) {
            fragmentShouru = new IncomeListFragment();
        }
        Bundle bundle1 = new Bundle();
        bundle1.putString("income_type", TYPE_INCOME);
        fragmentShouru.setArguments(bundle1);
        list.add(fragmentShouru);

        if (fragmentZhichu == null) {
            fragmentZhichu = new PayListFragment();
        }
        Bundle bundle2 = new Bundle();
        bundle2.putString("income_type", TYPE_ZHICHU);
        fragmentZhichu.setArguments(bundle2);
        list.add(fragmentZhichu);
    }

    private void setmViewPagerAdapter() {
        InComeViewPager inComeViewPager = new InComeViewPager(getSupportFragmentManager(), list);
        mViewPager.setAdapter(inComeViewPager);
        mViewPager.setCurrentItem(0);
    }

    private static final String TYPE_INCOME = "1";
    private static final String TYPE_ZHICHU = "2";

    private IncomeListFragment fragmentShouru;
    private PayListFragment fragmentZhichu;

    private void setShouru() {
        mViewPager.setCurrentItem(0);
    }

    private void setZhichu() {
        mViewPager.setCurrentItem(1);
    }

    @Override
    protected void onSetListener() {

        mRelativeListBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mTextShouru.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setShouru();
            }
        });
        mTextZhichu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setZhichu();
            }
        });

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                float now = position + positionOffset;
                mTopLine.setTranslationX((now - 1) * CommonUtil.getViewDistX(mTextZhichu, mTextShouru));
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private class InComeViewPager extends FragmentPagerAdapter {
        private List<Fragment> fragments;

        public InComeViewPager(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            this.fragments = fragments;
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }

}
